﻿using System;
using Caliburn.Micro;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Client.Specs.Support
{
    [TestClass]
    [Ignore]
    public class Specification
    {
        private Exception initializationException;
        private bool initialized = false;

        [TestInitialize]
        public void TestInitialize()
        {
            if (!initialized)
            {
                try
                {
                    if (IoC.BuildUp == null)
                    {
                        IoC.BuildUp = o => { };
                    }

                    EstablishContext();
                    Because();

                    initialized = true;
                }
                catch (Exception exception)
                {
                    // Somehow the unit testing harness eats exceptions in the TestInitialize, so we need to wrap them and
                    // throw them through a test method.
                    initializationException = exception;
                }
            }
        }

        [TestMethod]
        public void Establish_context_and_because()
        {
            if (initializationException != null)
            {
                throw initializationException;
            }
        }

        [TestCleanup]
        public void TestCleanup()
        {
            CleanupContext();
        }

        protected virtual void EstablishContext()
        {
        }

        protected virtual void Because()
        {
        }

        protected virtual void CleanupContext()
        {
        }
    }
}